#include "stdafx.h"
#include "ChangeAction.h"
#include "DataFlow.h"
#include "ArrowUnit.h"

ChangeAction::ChangeAction(int tempId, int tempOrder, CPoint preBegin,CPoint preEnd)
{
	preBeginPt = preBegin;
	preEndPt = preEnd;
	_m.id = tempId;
	_m.unit_type = tempOrder;
	_m.begin = findUnit(tempId)->m_BeginPoint;
	_m.end = findUnit(tempId)->m_EndPoint;
}

void ChangeAction::redo()
{
	theApp.m_UnitManager->Delete(findUnit(_m.id));
	//CPoint oldbegin=findUnit(_m.id)->m_BeginPoint;
	//CPoint oldend=findUnit(_m.id)->m_EndPoint;
	CPoint oldbegin=preBeginPt;
	CPoint oldend=preEndPt;
	findUnit(_m.id)->m_BeginPoint = _m.begin;
	findUnit(_m.id)->m_EndPoint = _m.end;
	if( _m.unit_type == ARROW_UNIT || _m.unit_type == ARROW2_UNIT)
	{
		theApp.m_UnitManager->AddArrow(findUnit(_m.id));
		if( _m.unit_type == ARROW2_UNIT )
			((CArrowUnit*)findUnit(_m.id))->EnableOptimize();
	}
	else
		theApp.m_UnitManager->AddShape(findUnit(_m.id));
	findUnit(_m.id)->AttachTracker();
	findUnit(_m.id)->TrackBroad(oldbegin,oldend);
}

void ChangeAction::undo()
{	
	theApp.m_UnitManager->Delete(findUnit(_m.id));
	CPoint oldbegin=findUnit(_m.id)->m_BeginPoint;
	CPoint oldend=findUnit(_m.id)->m_EndPoint;
	findUnit(_m.id)->m_BeginPoint = preBeginPt;
	findUnit(_m.id)->m_EndPoint = preEndPt;
	if( _m.unit_type == ARROW_UNIT || _m.unit_type == ARROW2_UNIT)
	{	
		theApp.m_UnitManager->AddArrow(findUnit(_m.id));
		if( _m.unit_type == ARROW2_UNIT )
			((CArrowUnit*)findUnit(_m.id))->EnableOptimize();
	}
	else
		theApp.m_UnitManager->AddShape(findUnit(_m.id));
	findUnit(_m.id)->AttachTracker();
	findUnit(_m.id)->TrackBroad(oldbegin,oldend);	
}